package com.vhall.component.service.rbac.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.rbac.RbacMenuMapper;
import com.vhall.component.dao.rbac.RbacRoleMenuesMapper;
import com.vhall.component.entity.account.MenuesEntity;
import com.vhall.component.entity.account.RoleMenuesEntity;
import com.vhall.component.entity.rbac.vo.MenuesRspVO;
import com.vhall.component.entity.rbac.vo.MenuesVO;
import com.vhall.component.service.rbac.RbacRoleMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author dalong
 * @date 2021-05-18 15:13
 */
@Service
public class RbacRoleMenuServiceImpl implements RbacRoleMenuService {

    @Autowired
    private RbacRoleMenuesMapper roleMenuesMapper;

    @Autowired
    private RbacMenuMapper menuMapper;

    @Override
    public List<Integer> getMenuIdListByRoleId(Integer roleId) {
        List<Integer> menuIdList = new ArrayList<>();
        LambdaQueryWrapper<RoleMenuesEntity> where = Wrappers.lambdaQuery();
        where.eq(RoleMenuesEntity::getRoleId, roleId)
                .isNull(RoleMenuesEntity::getDeletedAt);
        List<RoleMenuesEntity> list = roleMenuesMapper.selectList(where);
        if (CollUtil.isEmpty(list)) {
            return menuIdList;
        }
        menuIdList = list.stream().map(RoleMenuesEntity::getMenuId).collect(Collectors.toList());
        return menuIdList;
    }

    @Override
    public List<MenuesRspVO> getMenuesListByMenuIds(List<Integer> menuIdList, Integer roleId) {
        LinkedList<MenuesRspVO> menuesRspVOList = new LinkedList<>();
        if (CollUtil.isEmpty(menuIdList)) {
            return menuesRspVOList;
        }
        LambdaQueryWrapper<MenuesEntity> where = Wrappers.lambdaQuery();
        where.in(MenuesEntity::getMenuId, menuIdList)
                .isNull(MenuesEntity::getDeletedAt);
        List<MenuesEntity> list = menuMapper.selectList(where);
//        if (CollUtil.isEmpty(list)) {
//            throw new BusinessException(BizErrorCode.EMPTY_ROLE);
//        }

        for (MenuesEntity menuesEntity : list) {
            MenuesRspVO menuesRspVO = new MenuesRspVO();
            BeanUtils.copyProperties(menuesEntity, menuesRspVO);
            menuesRspVO.setRoleId(roleId);
            menuesRspVO.setSort(menuesEntity.getSort().intValue());
            menuesRspVOList.add(menuesRspVO);
        }
        Set<Integer> pidSet = menuesRspVOList.stream().map(MenuesRspVO::getPid).collect(Collectors.toSet());
        List<MenuesRspVO> handleMenusList = menuesRspVOList.stream().map(data -> {
            if (pidSet.contains(data.getMenuId())) {
                data.setUrl("");
            }
            return data;
        }).collect(Collectors.toList());
        return handleMenusList;
    }

    @Override
    public Integer insert(RoleMenuesEntity roleMenuesEntity) {
        return roleMenuesMapper.insert(roleMenuesEntity);
    }

    @Override
    public List<MenuesVO> getListByRoleId(Integer roleId) {
        List<MenuesEntity> menuEntities = new ArrayList<>();
        List<Integer> menuIdList = getMenuIdListByRoleId(roleId);
        if (CollUtil.isEmpty(menuIdList)) {
            LambdaQueryWrapper<MenuesEntity> where = Wrappers.lambdaQuery();
            where.in(MenuesEntity::getMenuId, menuIdList).isNull(MenuesEntity::getDeletedAt);
            menuEntities = menuMapper.selectList(where);
        }
        List<MenuesVO> menuesVOList = new LinkedList<>();
        for (MenuesEntity menuesEntity : menuEntities) {
            MenuesVO menuesVO = new MenuesVO();
            BeanUtils.copyProperties(menuesEntity, menuesVO);
            menuesVOList.add(menuesVO);
        }
        return menuesVOList;
    }

}
